<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import bg from "../assets/images/sign-bg.png";
    import msgIcon from "assets/images/help.png";
    import qrcodeBorder from "../assets/images/border-bg.png";
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";
    import constant from "constants/constant";
    import notification from "utils/notification";

    export default{
        data(){
            return {
                pageLoaded: false,//页面加载成功标识
                bg: bg,
                msgIcon: msgIcon, //问号按钮图片
                qrcodeBorder: qrcodeBorder,
                timeout: 3000, //单位时间为毫秒
                timer: null, //定时器
                isReturned: false, //轮询是否返回
                title: null, //标题
                info: [], //文案说明列表
                qrcodeUrl: null, //二维码地址
                remark: '', //说明文案
                signType: null //签约类型
            }
        },
        created() {
            //获取weex实例参数
            let config = weex.config;

            //签约类型
            if(typeof config.signType === 'undefined') {
                modal.toast('签约类型参数为空！');
                return false;
            }

            this.signType = config.signType;

            this.getQRCode();
        },
        methods: {
             /**
             * @description 获取免密支付签约的二维码
             */
            getQRCode(){
                let self = this;
                api.sendHttp("getSecurityQrcodeApi",{signtype: self.signType}).then(res =>{
                    let data = res.data || {};
                    self.pageLoaded = true;
                    self.qrcodeUrl = data.url || '';
                    self.remark = data.remark || '';
                    self.title = data.title || '';
                    self.info = data.info || [];

                    self.start();//启动监听支付成功接口
                },res => {
                    self.pageLoaded = true;
                    modal.toast(res.msg || '获取签约数据失败');
                })
            },

            /**
             * @description 定时器开始函数
             */
            start(){
                if(!this.timer) this.timer = setInterval(this.getSignInfo.bind(this), this.timeout);
            },

             /**
             * @description 查询签约免密支付信息函数
             */
            getSignInfo(){
                let self = this;
                api.sendHttp("getSecuritySignedApi",{signtype: self.signType,timeout: self.timeout},false).then(res=>{
                    let data = res.data || {};
                        if(data.status === '1') {
                            if(!self.isReturned) {
                                self.stop();
                                modal.toast(self.title + '已开通!');
                                navigator.pop();
                                notification.send(constant.EVENT_SECURITY_RELOAD, null);
                            }
                            self.isReturned = true; //轮询已返回
                        }
                },res=>{
                     console.log(res.msg);
                });
            },

            /**
             * @description 定时器结束函数
             */
            stop() {
                clearInterval(this.timer);
                this.timer = null;
            },

            /**
             * @description 查看免密支付说明
             */
            goToMsg() {
                let options = {
                    classType: constant.Drawer_Class_Type,
                    url: route.security_pay_msg,
                    title:  this.title + '说明',
                    params: {
                        info: this.info
                    }
                };
                navigator.push(options);
            }
        },
        destroyed() {
            this.stop();//组件销毁
        }
    }
</script>
